<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Message library class
 *
 * Message information's CRUD.
 *
 */
class Message_lib
{
	/**
	 * Constructor
	 *
	 * Load all message model.
	 */
	public function __construct()
	{
		$this->CI = &get_instance();

		$this->CI->load->model('pf/msg/msg_sys_model');
		$this->CI->load->model('pf/msg/user_sys_msg_model');
		$this->CI->load->model('pf/app/app_info_model');
	}

	/**
	 * Get system message regarding user id
	 *
	 * @param	integer	user id
	 * @return	object	system messages
	 */
	public function get_sys_msg($uid = 0, $appid = 0, $page = 0, $num = 20)
	{
		if($uid <= 0 || $appid <= 0)
			return array();
		
		$msgs = $this->CI->msg_sys_model->get_by_appids(array($appid, 0), $page, $num);
		$readmids = $this->CI->user_sys_msg_model->get_ids($uid);
		
		$list = array();
		foreach ($msgs as $msg)
		{
			$msg['is_read'] = 0;
			foreach ($readmids as $mid)
			{
				if($mid['msg_id'] == $msg['id'])
					$msg['is_read'] = 1;
			}
			unset($msg['app_id']);
			unset($msg['pic']);
			unset($msg['content']);
			unset($msg['url']);
			unset($msg['status']);
			
			array_push($list, $msg);
		}
		
		return $list;
	}
	
	/**
	 * Get system message regarding id
	 *
	 * @param	integer	message id
	 * @return	boolean
	 */
	public function get_sys_msg_by_id($msgid = 0)
	{
		if($msgid <= 0)
			return array();

		$msg = $this->CI->msg_sys_model->get_by_id($msgid);
		if(empty($msg))
			return array();
		
		if($msg['app_id'] == 0)
			$msg['from'] = 'kick9';
		else
		{
			$game = $this->CI->app_info_model->get_app($msg['app_id']);
			if(isset($game['name']))
				$msg['from'] = $game['name'];
		}

		if($msg['type'] == 1)
		{
			if(!$msg['pic']){
				unset($msg['pic']);
			}else{
				$msg['pic'] = $this->CI->config->item('res_domain').$msg['pic'];
			}
			unset($msg['url']);
		}
		elseif($msg['type'] == 2)
		{
			unset($msg['pic']);
			unset($msg['content']);
		}

		unset($msg['status']);
		return $msg;
	}
	
	/**
	 * Set system message read status regarding id
	 *
	 * @param	integer	message id
	 * @return	boolean
	 */
	public function set_sys_msg_readed($uid = 0, $appid = 0, $msgid = 0)
	{
		if($uid <= 0 || $appid <= 0 || $msgid <= 0)
			return false;
	
		$msg = $this->CI->msg_sys_model->get_by_id($msgid);
		if(empty($msg))
			return false;
		
		if($msg['app_id'] != $appid && $msg['app_id'] > 0)
			return false;
		
		$ret = $this->CI->user_sys_msg_model->get($uid, $msgid);
		if(empty($ret))
			$this->CI->user_sys_msg_model->add(array('user_id' => $uid, 'msg_id' => $msgid));
	
		return true;
	}
	
	/**
	 * Get unread message number regarding id
	 *
	 * @param	integer	user id
	 * @param	integer	application id
	 * @return	integer
	 */
	public function get_unread_num($uid = 0, $appid = 0)
	{
		if($uid <= 0 || $appid <= 0)
			return 0;
	
		$this->CI->load->helper('db');
		$user_sys_msg_table = get_dtable('user_sys_msg_', $uid);
		$sql = 'SELECT COUNT(c.id) FROM
				(
				SELECT a.*, b.msg_id tmpcol FROM msg_sys a LEFT JOIN '.$user_sys_msg_table.
				' b ON a.id=b.msg_id AND a.status=1 AND b.user_id='.$uid.'
				) c
				WHERE c.tmpcol IS NULL AND c.app_id IN ('.$appid.', 0)
				ORDER BY c.id';
		$ret = $this->CI->msg_sys_model->get_db_r()->query($sql)->row_array();
		
		return $ret['COUNT(c.id)'];
	}
}